<template>
  <div class="container">
    <div class="app-container">
      <el-button size="mini" type="primary" class="btn-add" @click="addBtn">添加权限</el-button>
      <el-table :data="list" row-key="id" default-expand-all>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="标识" prop="code"></el-table-column>
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" size="mini" @click="add(row.id)">添加</el-button>
            <el-button type="text" size="mini" @click="editBtn(row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    放置弹层-->
    <el-dialog :title="idEdit?'编辑权限':'添加权限'" :visible="showDialog" @close="close">
      <el-form label-width="120px" :model="perForm" :rules="perRules" ref="perForm">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="perForm.name" size="mini" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="perForm.code" size="mini" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="perForm.description" type="textarea" size="mini" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch v-model="perForm.enVisible" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
              <el-button size="mini" @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPermissionList,
  getAddPermission,
  getEditPermission,
  getDetailPermission,
  delPermission
} from '@/api/permission'
import {toTreeDate} from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      idEdit: false,
      list: [],
      id: null, // 区分两个添加
      perForm: {
        name: '',
        code: '',
        description: '',
        type: 1,
        pid: 0,
        enVisible: '0' // 0关闭 1开启
      },
      perRules: {
        name: [
          {
            required: true,
            message: '权限名称不能为空',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '权限标识不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  computed: {},
  methods: {
    addBtn() {
      this.id = null
      this.idEdit = false
      this.showDialog = true
    },
    add(id) {
      this.id = id
      this.idEdit = false
      this.showDialog = true
    },
    async editBtn(id) {
      this.idEdit = true
      this.showDialog = true
      const res = await getDetailPermission(id)
      console.log(res)
      this.perForm = res
    },
    async btnOk() {
      this.$refs.perForm.validate(async isOk => {
        if (isOk) {
          if (this.perForm.id) {
            await getEditPermission(this.perForm)
            this.$message.success('编辑权限成功')
          } else {
            if (this.id) {
              await getAddPermission({
                ...this.perForm,
                pid: this.id,
                type: 2
              })
              this.$message.success('添加权限成功')
            } else {
              await getAddPermission(this.perForm)
              this.$message.success('添加权限成功')
            }
          }
          this.getPermissionList()
          this.close()
        }
      })
    },
    async del(id) {
      // 删除
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delPermission(id)
        this.$message({
          type: 'success',
          message: '删除权限成功!'
        })
        this.getPermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    close() {
      // resetFields 只能重置模板中绑定的数据 id清不掉
      this.perForm = {
        code: '',
        name: '',
        description: '',
        enVisible: 0
      }
      this.$refs.perForm.resetFields()
      this.showDialog = false
    },
    async getPermissionList() {
      this.list = toTreeDate(await getPermissionList(), 0)
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
